<?php
// 显示所有错误
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// 检查必要文件是否存在
echo "<h2>检查必要文件</h2>";
$required_files = [
    '../config/database.php',
    'includes/header.php',
    'includes/footer.php',
    'includes/functions.php',
    'includes/AdminAuth.php',
    'index.php',
    'users.php',
    'games.php',
    'achievements.php',
    'stats.php',
    'settings.php'
];

$allFilesExist = true;
foreach ($required_files as $file) {
    if (file_exists($file)) {
        echo "✅ 文件 <code>" . htmlspecialchars($file) . "</code> 存在<br>";
    } else {
        echo "❌ 文件 <code>" . htmlspecialchars($file) . "</code> <strong>不存在</strong><br>";
        $allFilesExist = false;
    }
}

if (!$allFilesExist) {
    echo "<div style='color: red; font-weight: bold;'>有必要文件不存在，请先确保所有文件都已创建。</div>";
}

// 检查数据库连接和表是否存在
echo "<h2>检查数据库连接</h2>";
try {
    require_once '../config/database.php';
    
    $dsn = "mysql:host=".DB_HOST.";dbname=".DB_NAME.";charset=".DB_CHARSET;
    $pdo = new PDO($dsn, DB_USERNAME, DB_PASSWORD);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    echo "✅ 数据库连接成功<br>";
    
    // 检查表是否存在
    $tables = [
        DB_PREFIX.'users',
        DB_PREFIX.'games',
        DB_PREFIX.'achievements',
        DB_PREFIX.'game_records',
        DB_PREFIX.'user_achievements',
        DB_PREFIX.'settings'
    ];
    
    $allTablesExist = true;
    foreach ($tables as $table) {
        $stmt = $pdo->query("SHOW TABLES LIKE '{$table}'");
        if ($stmt->rowCount() > 0) {
            echo "✅ 表 <code>" . htmlspecialchars($table) . "</code> 存在<br>";
            
            // 检查表中的数据
            $count = $pdo->query("SELECT COUNT(*) FROM {$table}")->fetchColumn();
            echo "&nbsp;&nbsp;&nbsp;└─ 包含 {$count} 条记录<br>";
        } else {
            echo "❌ 表 <code>" . htmlspecialchars($table) . "</code> <strong>不存在</strong><br>";
            $allTablesExist = false;
        }
    }
    
    if (!$allTablesExist) {
        echo "<div style='color: red; font-weight: bold;'>有必要的数据表不存在，请先运行 setup_tables.php 创建数据表。</div>";
    }
    
} catch (PDOException $e) {
    echo "❌ 数据库连接失败：" . $e->getMessage() . "<br>";
}

// 检查页面加载
echo "<h2>检查页面加载</h2>";
echo "以下是每个页面可能存在的问题：<br>";

// 获取页面错误
function check_page($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    $output = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    
    if ($httpCode == 200) {
        // 检查页面内容是否为空
        if (empty(trim($output))) {
            return "返回空白页面";
        }
        // 检查是否包含错误
        elseif (stripos($output, 'error') !== false || stripos($output, 'warning') !== false || stripos($output, 'notice') !== false) {
            return "可能含有PHP错误";
        } else {
            return "正常";
        }
    } else {
        return "HTTP错误 {$httpCode}";
    }
}

$pages = [
    'index.php' => '控制台',
    'users.php' => '用户管理',
    'games.php' => '游戏管理',
    'achievements.php' => '成就管理',
    'stats.php' => '数据统计',
    'settings.php' => '系统设置'
];

$base_url = 'http://127.0.0.1/admin/';
$pages_status = [];

foreach ($pages as $page => $description) {
    $url = $base_url . $page;
    $status = check_page($url);
    $pages_status[$page] = $status;
    
    $icon = ($status == "正常") ? "✅" : "⚠️";
    echo "{$icon} <strong>{$description}</strong> ({$page}): {$status}<br>";
}

// 检查JS和CSS文件
echo "<h2>检查资源文件</h2>";
$resources = [
    'css/admin.css' => 'CSS样式表',
    'js/admin.js' => 'JavaScript脚本'
];

foreach ($resources as $file => $description) {
    if (file_exists($file)) {
        echo "✅ {$description} (<code>{$file}</code>) 存在<br>";
    } else {
        echo "❌ {$description} (<code>{$file}</code>) <strong>不存在</strong><br>";
    }
}

// 提供解决方案
echo "<h2>建议解决方案</h2>";

if (!$allFilesExist || !$allTablesExist) {
    echo "<div style='color: red;'>请先确保所有必要文件和数据表都已创建。</div>";
} else {
    $issues = false;
    foreach ($pages_status as $page => $status) {
        if ($status != "正常") {
            $issues = true;
            break;
        }
    }
    
    if ($issues) {
        echo "1. 尝试运行 <a href='add_test_data.php' target='_blank'>add_test_data.php</a> 添加测试数据<br>";
        echo "2. 在浏览器中打开开发者工具 (F12)，查看是否有JavaScript错误<br>";
        echo "3. 检查服务器错误日志获取更多信息<br>";
    } else {
        echo "<div style='color: green;'>所有页面似乎都能正常加载。如果您仍然看不到数据，请确保：</div>";
        echo "1. 您已运行 <a href='add_test_data.php' target='_blank'>add_test_data.php</a> 添加测试数据<br>";
        echo "2. 您已正确登录管理员账户<br>";
        echo "3. 您的浏览器没有禁用JavaScript<br>";
    }
}

?>

<div style="margin-top: 20px;">
    <a href="add_test_data.php" class="btn btn-primary">添加测试数据</a>
    <a href="index.php" class="btn btn-secondary">返回管理后台</a>
</div>

<style>
    body {
        font-family: Arial, sans-serif;
        line-height: 1.6;
        margin: 20px;
    }
    h2 {
        margin-top: 20px;
        padding-bottom: 5px;
        border-bottom: 1px solid #ddd;
    }
    .btn {
        display: inline-block;
        padding: 8px 16px;
        margin-right: 10px;
        text-decoration: none;
        border-radius: 4px;
        color: white;
    }
    .btn-primary {
        background-color: #007bff;
    }
    .btn-secondary {
        background-color: #6c757d;
    }
</style> 